package com.aaa.sbm.service.impl;

import com.aaa.sbm.common.ResultUtil;
import com.aaa.sbm.dao.ProductDao;
import com.aaa.sbm.dao.StockInApprovalDao;
import com.aaa.sbm.dao.StockInDao;
import com.aaa.sbm.dao.StockInDetailDao;
import com.aaa.sbm.entity.Product;
import com.aaa.sbm.entity.StockIn;
import com.aaa.sbm.entity.StockInApproval;
import com.aaa.sbm.entity.StockInDetail;
import com.aaa.sbm.service.StockInApprovalService;
import com.aaa.sbm.util.Page;
import com.aaa.sbm.util.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class StockInApprovalServiceImpl implements StockInApprovalService {

    @Resource
    private StockInApprovalDao stockInApprovalDao;

    @Resource
    private StockInDao stockInDao;

    @Resource
    private StockInDetailDao stockInDetailDao;

    @Resource
    private ProductDao productDao;

    @Override
    public Result getStockList(Page<StockInApproval>  page) {
        PageHelper.startPage(page.getPageNo(), page.getPageSize());
        List<StockInApproval> stockInApprovals = stockInApprovalDao.selectList(page.getData());
        PageInfo<StockInApproval> pageInfo = new PageInfo<>(stockInApprovals);
        return ResultUtil.success(pageInfo);
    }

    @Override
    public Result updateStockIn(StockInApproval stockInApproval) {
        StockInDetail stockInDetail = new StockInDetail();
        stockInDetail.setId(stockInApproval.getSdId());
        StockInDetail stockInDetail1 = stockInDetailDao.selectById(stockInApproval.getSdId());
        StockIn stockIn = new StockIn();
        stockIn.setId(stockInDetail1.getInId());
        stockIn.setStatus(0);
        int i = stockInDao.updateStockIn(stockIn);
        Product product = productDao.selectById(stockInApproval.getProductId());
        int i1 = productDao.updateStock(stockInApproval.getProductId(), product.getStock()+stockInApproval.getQuantity());
        stockInApproval.setStatus(1);
        int a = stockInApprovalDao.updateStockInApproval(stockInApproval);
        return a + i>0?ResultUtil.success("审核通过") : ResultUtil.fail("系统错误");
    }

    @Override
    public Result updateStockIn2(StockInApproval stockInApproval) {
        StockInDetail stockInDetail = new StockInDetail();
        stockInDetail.setId(stockInApproval.getSdId());
        StockInDetail stockInDetail1 = stockInDetailDao.selectById(stockInApproval.getSdId());
        StockIn stockIn = new StockIn();
        stockIn.setId(stockInDetail1.getInId());
        stockIn.setStatus(1);
        int i = stockInDao.updateStockIn(stockIn);
        stockInApproval.setStatus(1);
        int a = stockInApprovalDao.updateStockInApproval(stockInApproval);
        return a + i>0?ResultUtil.success("审核不通过") : ResultUtil.fail("系统错误");
    }
}
